<template>
  <Head />
  <div v-for="(item, index) in walletList" :key="index" class="wallet">
    <img :src="item.logo" alt="" class="logo" />
    <div class="content">
      <div class="top">
        <span>{{ item.symbol }}</span>
        <span>{{ formatMoney(item.balance) }}</span>
      </div>
      <div class="bottom">
        <span>{{ item.exchangeSymbol }}</span>
        <span
          >≈$ {{ formatMoney(item.exchangeAmount) }}
          <i
            :class="[
              'exchangeRate',
              Number(item.exchangeRate) > 0 ? 'green' : 'red'
            ]"
            >（$
            {{
              Number(item.exchangeRate) > 0
                ? `+${formatMoney(item.exchangeRate)}`
                : formatMoney(item.exchangeRate)
            }}）</i
          >
        </span>
      </div>
    </div>
    <img
      class="donate"
      :src="
        item.balance == 0
          ? getAssetsFile('no-donate.png')
          : getAssetsFile('donate.png')
      "
      alt=""
      @click="onDonate(item)"
    />
  </div>

  <van-popup
    v-model:show="showBottom"
    round
    position="bottom"
    :style="{ height: '30%', padding: '12px', background: '#f2f2f2' }"
    class="donate-popup"
    :closeable="true"
  >
    <div class="title">{{ $t("donate") }}</div>
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field
          :label-width="60"
          v-model="amount"
          :name="$t('donationNum')"
          :label="$t('donationNum')"
          :placeholder="$t('pleaseDonationNum')"
        />
      </van-cell-group>
      <div class="submit">
        <van-button
          class="btn"
          @click="onSubmit"
          :loading="loading"
          :loading-text="$t('confirm')"
        >
          {{ $t("confirm") }}
        </van-button>
      </div>
    </van-form>
  </van-popup>
</template>

<script setup lang="ts">
import { getAssetsFile, formatMoney } from "@/utils/tool";
import { Async_myWallet, Async_donate } from "@/server/user";
import { IWalletItem } from "@/model/user";
import { showToast } from "vant";
import { useI18n } from "vue-i18n";
const { t } = useI18n();
const walletList = ref<IWalletItem[]>([]);
const walletInfo = ref<IWalletItem>();
const showBottom = ref(false);
const amount = ref("");
const loading = ref(false);

const getList = async () => {
  try {
    let res: IWalletItem[] = await Async_myWallet();
    if (res) {
      walletList.value = res || [];
    }
  } catch (error) {
    console.error(error);
  }
};

const onDonate = (item: IWalletItem) => {
  if (item.balance == 0) return false;
  walletInfo.value = item;
  showBottom.value = true;
};

const onSubmit = async () => {
  try {
    if (!amount.value) {
      return showToast(t("pleaseDonationNum"));
    }
    loading.value = true;
    let res = await Async_donate({
      coin: walletInfo.value.symbol,
      amount: Number(amount.value)
    });
    if (res) {
      showBottom.value = false;
      showToast(t("successOperation"));
    }
  } catch (error) {
    console.error(error);
  } finally {
    loading.value = false;
  }
};

onMounted(() => {
  getList();
});
</script>
<style scoped lang="scss">
.wallet {
  display: flex;
  justify-content: flex-start;
  padding: 12px;
  border-bottom: 1px solid #1c1c1e;
  .logo {
    width: 45px;
    height: 45px;
    border-radius: 50%;
    margin-right: 12px;
    border: 1px solid #060b10;
  }
  .content {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;

    .top {
      font-size: 16px;
      color: #fff;
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
    .bottom {
      font-size: 12px;
      color: #777779;
      width: 100%;
      display: flex;
      justify-content: space-between;
      .diffFee {
        font-style: normal;
        &.red {
          color: red;
        }
        &.green {
          color: green;
        }
      }
    }
  }
  .donate {
    width: 30px;
    margin-left: 12px;
  }
}

.van-popup.donate-popup {
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  .title {
    color: #000;
    text-align: center;
    margin-bottom: 20px;
  }
  .submit {
    width: 100%;
    padding: 10px 16px;
    display: flex;
    align-items: center;
    margin-top: 20px;
    .btn {
      width: 100%;
      height: 42px;
      background: linear-gradient(to right, #ffe07b, #e8a22d);
      font-size: 15px;
      font-family: PingFangSC-Semibold, PingFang SC;
      font-weight: 600;
      color: #ffffff;
      border-radius: 20px;
      letter-spacing: 5px;
    }
  }
}
</style>
